<?php defined('BASEPATH') OR die('No direct access allowed.');
/**
 * Description of "Common_model"
 *
 * Created on Dec 20, 2010
 * @author Ramon Gutierrez
 * @copyright Bitstop Network Services Inc.
 * @version 0.1
 */

class Common_model extends Model {
	public function Common_model() {
        parent::Model();
		$this->CI =& get_instance();
    }
	
	// Get all countries
	public function getCountries($fields = "*") {
		$query = $this->db->select($fields)->order_by('name', 'asc')->get('countries');
		return $query;
	}
	
	// Get all companies
	public function getAllCompanies($fields = "*") {
		$query = $this->db->select($fields)->order_by('name', 'asc')->get('clients');
		return $query;
	}
	
	// Get all companies matching a certain string
	public function getCompanies($s) {
		$query = $this->db->query("SELECT c.id, c.name, CONCAT(a.first_name, ' ', a.last_name) AS owner FROM clients c, accounts a WHERE a.id = c.account_id AND c.name LIKE '%$s%'");
		if($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}
	
	// Get all industry types
	public function getIndustryTypes($fields = "*") {
		$query = $this->db->select($fields)->order_by('title', 'asc')->get('industry_types');
		return $query;
	}
	
	// Get row info
	public function getInfo($table, $id, $fields = "*", $where = false) {
		$this->db->select($fields);
		$query = $this->db->get_where($table, $where ? $where : array('id' => $id));
		
		if($query->num_rows() > 0) {
			$row = $query->row_array();
			return $row;
		} else {
			return false;
		}
	}
	
	// Get row name
	public function getName($table, $id, $column = "name") {
		$this->db->select($column);
		$query = $this->db->get_where($table, array('id' => $id));
		
		if($query->num_rows() > 0) {
			$row = $query->row();
			return $row->$column;
		} else {
			return false;
		}
	}
	
	// Check if exists
	public function exists($table, $id = false, $where = false) {
		$this->db->select('id');
		
		if($where) {
			$this->db->where($where);
		} else if($id) {
			$this->db->where('id', $id);
		} else {
			return false;
		}
		
		$query = $this->db->get($table);
		return $query->num_rows();
	}
	
	// Check if logged in
	public function isOnline($id) {
		$query = $this->db->query("SELECT session_id FROM ci_sessions WHERE user_data LIKE '%" . md5('~qsysb~' . $id) . "%' AND ip_address != '" . $_SERVER['REMOTE_ADDR'] . "'");
		return $query->num_rows();
	}
	
	// Update a row
	public function update($table, $id = false, $data, $where = false) {
		if($where) {
			$this->db->where($where);
		} else if($id) {
			$this->db->where('id', $id);
		} else {
			return false;
		}
		
		$query = $this->db->update($table, $data);
		return $query;
	}
	
	// Create a new row
	public function create($table, $data) {
		$query = $this->db->insert($table, $data);
		
		if($query) {
			return $this->db->insert_id();
		} else {
			return false;
		}
	}
	
	// Delete a row
	public function delete($table, $id = false, $where = false) {
		if($id) {
			$where = array('id' => $id);
		} else if(!$where) {
			return false;
		}
		
		$query = $this->db->delete($table, $where);
		return $query;
	}
	
	// Generate unique client validation key
	public function create_validation() {
		$dupe = true;
	
		while($dupe) {
			$key = strtoupper("QP" . rand_str(3) . "-" . rand_str(5) . "-" . rand_str(5));
			
			// Check if unique
			$dupe = $this->db->query("SELECT id FROM clients WHERE validation_key = '$key'")->num_rows();
		}
		
		return $key;
	}
	
	// Generate unique product key
	public function create_key() {
		$dupe = true;
		
		while($dupe) {
			$key = strtoupper("QU" . rand_str(3) . "-" . rand_str(5) . "-" . rand_str(5) . "-" . rand_str(5) . "-" . rand_str(5));
			
			// Check if unique
			$dupe = $this->db->query("SELECT id FROM branches WHERE product_key = '$key'")->num_rows();
		}
		
		return $key;
	}
}

/* End of file common_model.php */
/* Location: ./system/application/models/common_model.php */